#!/usr/bin/python3
# Example file showing a basic pygame "game loop"
import pygame
import serial
import pygame_gui
import serial.tools.list_ports
import time
import layout

# pygame setup
pygame.init()
screen = pygame.display.set_mode((1920, 1080))
clock = pygame.time.Clock()
running = True
# history = []

# manager = pygame_gui.UIManager((1920, 1080), './theme.json')
background = pygame.Surface((1920, 1080))

pygame.display.flip()

options = serial.tools.list_ports.comports()
ports = [i.device for i in options]
names = [i.description for i in options]
print(ports, names)
esp32c3 = None

if len(ports) == 0:
    print('no esp32')
else:
    esp32c3 = serial.Serial(ports[0], 115200)

button_row_width = 400
button_row_height = 160
zx,zy = 70, 17
zx1,zy1 = 70,720

# position1 = (zx, zy)
# button1 = pygame_gui.elements.UIButton(relative_rect=pygame.Rect(position1,
#                                                                (button_row_width,
#                                                                 button_row_height)),
#                                      text = "stp_moto_1",
#                                      manager=manager,
#                                      object_id='#1')
# position2 = (zx, zy + button_row_height + 17)
# button2 = pygame_gui.elements.UIButton(relative_rect=pygame.Rect(position2,
#                                                                (button_row_width,
#                                                                 button_row_height)),
#                                      text = "stp_moto_2",
#                                      manager=manager,
#                                      object_id='#2')
# position3 = (zx + button_row_width + 160 // 3, zy)
# button3 = pygame_gui.elements.UIButton(relative_rect=pygame.Rect(position3,
#                                                                (button_row_width,
#                                                                 button_row_height)),
#                                      text = "DC_moto_1",
#                                      manager=manager,
#                                      object_id='#3')
# position4 = (zx + button_row_width + 160 // 3, zy + button_row_height + 17)
# button4 = pygame_gui.elements.UIButton(relative_rect=pygame.Rect(position4,
#                                                                (button_row_width,
#                                                                 button_row_height)),
#                                      text = "DC_moto_2",
#                                      manager=manager,
#                                      object_id='#4')
# position5 = (zx + (button_row_width + 160 // 3) * 2, zy )
# button5 = pygame_gui.elements.UIButton(relative_rect=pygame.Rect(position5,
#                                                                (button_row_width,
#                                                                 button_row_height)),
#                                      text = "DC_moto_3",
#                                      manager=manager,
#                                      object_id='#5')
# position6 = (zx + (button_row_width + 160 // 3) * 2, zy + button_row_height + 17)
# button6 = pygame_gui.elements.UIButton(relative_rect=pygame.Rect(position6,
#                                                                (button_row_width,
#                                                                 button_row_height)),
#                                      text = "DC_moto_4",
#                                      manager=manager,
#                                      object_id='#6')
# position7 = (zx + (button_row_width + 160 // 3) * 3, zy )
# button7 = pygame_gui.elements.UIButton(relative_rect=pygame.Rect(position7,
#                                                                (button_row_width,
#                                                                 button_row_height)),
#                                      text = "ele_moto_1",
#                                      manager=manager,
#                                      object_id='#7')
# position8 = (zx + (button_row_width + 160 // 3) * 3, zy + button_row_height + 17)
# button8 = pygame_gui.elements.UIButton(relative_rect=pygame.Rect(position8,
#                                                                (button_row_width,
#                                                                 button_row_height)),
#                                      text = "ele_moto_2",
#                                      manager=manager,
#                                      object_id='#8')
# position9 = (zx1, zy1)
# button1_1 = pygame_gui.elements.UIButton(relative_rect=pygame.Rect(position9,
#                                                                (button_row_width,
#                                                                 button_row_height)),
#                                      text = "stp_moto_1",
#                                      manager=manager,
#                                      object_id='#1')
# position10 = (zx1, zy1 + button_row_height + 17)
# button2_1 = pygame_gui.elements.UIButton(relative_rect=pygame.Rect(position10,
#                                                                (button_row_width,
#                                                                 button_row_height)),
#                                      text = "stp_moto_2",
#                                      manager=manager,
#                                      object_id='#2')
# position11 = (zx1 + button_row_width + 160 // 3, zy1)
# button3_1 = pygame_gui.elements.UIButton(relative_rect=pygame.Rect(position11,
#                                                                (button_row_width,
#                                                                 button_row_height)),
#                                      text = "DC_moto_1",
#                                      manager=manager,
#                                      object_id='#3')
# position12 = (zx1 + button_row_width + 160 // 3, zy1 + button_row_height + 17)
# button4_1 = pygame_gui.elements.UIButton(relative_rect=pygame.Rect(position12,
#                                                                (button_row_width,
#                                                                 button_row_height)),
#                                      text = "DC_moto_2",
#                                      manager=manager,
#                                      object_id='#4')
# position13 = (zx1 + (button_row_width + 160 // 3) * 2, zy1 )
# button5_1 = pygame_gui.elements.UIButton(relative_rect=pygame.Rect(position13,
#                                                                (button_row_width,
#                                                                 button_row_height)),
#                                      text = "DC_moto_3",
#                                      manager=manager,
#                                      object_id='#5')
# position14 = (zx1 + (button_row_width + 160 // 3) * 2, zy1 + button_row_height + 17)
# button6_1 = pygame_gui.elements.UIButton(relative_rect=pygame.Rect(position14,
#                                                                (button_row_width,
#                                                                 button_row_height)),
#                                      text = "DC_moto_4",
#                                      manager=manager,
#                                      object_id='#6')
# position15 = (zx1 + (button_row_width + 160 // 3) * 3, zy1 )
# button7_1 = pygame_gui.elements.UIButton(relative_rect=pygame.Rect(position15,
#                                                                (button_row_width,
#                                                                 button_row_height)),
#                                      text = "ele_moto_1",
#                                      manager=manager,
#                                      object_id='#7')
# position16 = (zx1 + (button_row_width + 160 // 3) * 3, zy1 + button_row_height + 17)
# button8_1 = pygame_gui.elements.UIButton(relative_rect=pygame.Rect(position16,
#                                                                (button_row_width,
#                                                                 button_row_height)),
#                                      text = "ele_moto_2",
#                                      manager=manager,
#                                      object_id='#8')
# position17 = (zx, 371)
# button17 = pygame_gui.elements.UIButton(relative_rect=pygame.Rect(position17,
#                                                                (button_row_width,
#                                                                 button_row_height)),
#                                      text = "spd+",
#                                      manager=manager,
#                                      object_id='#9')
# position18 = (zx, 548)
# button18 = pygame_gui.elements.UIButton(relative_rect=pygame.Rect(position18,
#                                                                (button_row_width,
#                                                                 button_row_height)),
#                                      text = "spd-",
#                                      manager=manager,
#                                      object_id='#9')
# position19 = (1920 / 2 - button_row_width /2 , 371)
# button119 = pygame_gui.elements.UIButton(relative_rect=pygame.Rect(position19,
#                                                                (button_row_width,
#                                                                 button_row_height)),
#                                      text = "run+",
#                                      manager=manager,
#                                      object_id='#10')
# position20 = (1920 / 2 - button_row_width /2, 548)
# button20 = pygame_gui.elements.UIButton(relative_rect=pygame.Rect(position20,
#                                                                (button_row_width,
#                                                                 button_row_height)),
#                                      text = "run-",
#                                      manager=manager,
#                                      object_id='#10')
# position21 = (zx + (button_row_width + 160 // 3) * 3, 371)
# button21 = pygame_gui.elements.UIButton(relative_rect=pygame.Rect(position21,
#                                                                (button_row_width,
#                                                                 button_row_height)),
#                                      text = "up",
#                                      manager=manager,
#                                      object_id='#11')
# position22 = (zx + (button_row_width + 160 // 3) * 3, 548)
# button22 = pygame_gui.elements.UIButton(relative_rect=pygame.Rect(position22,
#                                                                (button_row_width,
#                                                                 button_row_height)),
#                                      text = "down",
#                                      manager=manager,
#                                      object_id='#11')

count1,count2,count3,count4,count5,count6,count7,count8 = 0,0,0,0,0,0,0,0
count1_1,count2_1,count3_1,count4_1,count5_1,count6_1,count7_1,count8_1 = 0,0,0,0,0,0,0,0
dir1,dir2,dir3,dir4,dir5,dir6,dir7,dir8 = 1,1,1,1,1,1,1,1
spd1 = 19200
run1 = 512

while running:
    time_delta = clock.tick(60)/1000.0
    # poll for events
    # pygame.QUIT event means the user clicked X to close your window
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        layout.manager.process_events(event)

        ##==========================stp_moto_1==============================================##

        if event.type == pygame_gui.UI_BUTTON_START_PRESS:      #检测步进电机1 正转按钮按下
            if event.ui_element is button1:          #正转按钮按下    按第一下转   第二下停
                count1 += 1
                if count1 % 2 != 0:
                    if esp32c3 is not None:
                        dir1 = 1
                        command  = f":0 {spd1}\n"
                        esp32c3.write(command.encode())
                        print(spd1 ,'begin')
                elif count1 % 2 == 0:
                    if esp32c3 is not None:
                        command  = f":0 0\n"
                        esp32c3.write(command.encode())
                        print('stop')
            if event.ui_element is button1_1:
                count1_1 -= 1
                if count1_1 % 2 != 0:
                    if esp32c3 is not None:
                        dir1 = -1
                        command  = f":0 {-spd1}\n"
                        esp32c3.write(command.encode())
                        print(-spd1 ,'begin')
                elif count1_1 % 2 == 0:
                    if esp32c3 is not None:
                        command  = f":0 0\n"
                        esp32c3.write(command.encode())
                        print('stop')
            if event.type == pygame_gui.UI_BUTTON_START_PRESS:
                if count1 % 2 | count1_1 % 2 != 0:
                    if esp32c3 is not None:
                        if event.ui_element is button17:          #检测步进电机1 加速按钮按下
                            spd1 += 6400
                            command = f":0 {dir1 * spd1}\n"
                            esp32c3.write(command.encode())
                            print(dir1 * spd1, 'up')
                        elif event.ui_element is button18:        #检测步进电机1 减速按钮按下
                            spd1 -= 6400
                            command = f":0 {dir1 * spd1}\n"
                            esp32c3.write(command.encode())
                            print(dir1 * spd1, 'down')

        ##==========================stp_moto_2==============================================##
                            
        if event.type == pygame_gui.UI_BUTTON_START_PRESS:      #检测步进电机2 正转按钮按下
            if event.ui_element is button2:          #正转按钮按下    按第一下转   第二下停
                count2 += 1
                if count2 % 2 != 0:
                    if esp32c3 is not None:
                        dir2 = 1
                        command  = f":1 {spd1}\n"
                        esp32c3.write(command.encode())
                        print(spd1 ,'begin')
                elif count2 % 2 == 0:
                    if esp32c3 is not None:
                        command  = f":1 0\n"
                        esp32c3.write(command.encode())
                        print('stop')
            if event.ui_element is button2_1:
                count2_1 -= 1
                if count2_1 % 2 != 0:
                    if esp32c3 is not None:
                        dir2 = -1
                        command  = f":1 {-spd1}\n"
                        esp32c3.write(command.encode())
                        print(-spd1 ,'begin')
                elif count2_1 % 2 == 0:
                    if esp32c3 is not None:
                        command  = f":1 0\n"
                        esp32c3.write(command.encode())
                        print('stop')
            if event.type == pygame_gui.UI_BUTTON_START_PRESS:
                if count2 % 2 | count2_1 % 2 != 0:
                    if esp32c3 is not None:
                        if event.ui_element is button17:          #检测步进电机1 加速按钮按下
                            spd1 += 6400
                            command = f":1 {dir2 * spd1}\n"
                            esp32c3.write(command.encode())
                            print(dir2 * spd1, 'up')
                        elif event.ui_element is button18:        #检测步进电机1 减速按钮按下
                            spd1 -= 6400
                            command = f":1 {dir2 * spd1}\n"
                            esp32c3.write(command.encode())
                            print(dir2 * spd1, 'down')      

             ##==========================DC_moto_1==============================================##   

        if event.type == pygame_gui.UI_BUTTON_START_PRESS:      #检测步进电机2 正转按钮按下
            if event.ui_element is button3:          #正转按钮按下    按第一下转   第二下停
                count3 += 1
                if count3 % 2 != 0:
                    if esp32c3 is not None:
                        dir3 = 1
                        command  = f":2 {run1}\n"
                        esp32c3.write(command.encode())
                        print(run1 ,'begin')
                elif count3 % 2 == 0:
                    if esp32c3 is not None:
                        command  = f":2 0\n"
                        esp32c3.write(command.encode())
                        print('stop')
            if event.ui_element is button3_1:
                count3_1 -= 1
                if count3_1 % 2 != 0:
                    if esp32c3 is not None:
                        dir3 = -1
                        command  = f":2 {-run1}\n"
                        esp32c3.write(command.encode())
                        print(-run1 ,'begin')
                elif count3_1 % 2 == 0:
                    if esp32c3 is not None:
                        command  = f":2 0\n"
                        esp32c3.write(command.encode())
                        print('stop')
            if event.type == pygame_gui.UI_BUTTON_START_PRESS:
                if count3 % 2 | count3_1 % 2 != 0:
                    if esp32c3 is not None:
                        if event.ui_element is button19:          #检测步进电机1 加速按钮按下
                            run1 += 
                            command = f":1 {dir2 * spd1}\n"
                            esp32c3.write(command.encode())
                            print(dir2 * spd1, 'up')
                        elif event.ui_element is button18:        #检测步进电机1 减速按钮按下
                            spd1 -= 6400
                            command = f":1 {dir2 * spd1}\n"
                            esp32c3.write(command.encode())
                            print(dir2 * spd1, 'down')
                   
                            
    manager.update(time_delta)

    screen.blit(background, (0, 0))
    manager.draw_ui(screen)
    pygame.display.flip()

    clock.tick(60)  # limits FPS to 60

pygame.quit()

